﻿using Common.Communication;
using Common.Device.DeviceDb;
using Common.Device.Enums;
using Common.Device.Interfaces;
using Common.Tool.DataTools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CeLiangYi.WanYongBiao
{
    public class DM3058ENiVisa:NiVisaBase , IWanYongBiao
    {
        public string Name { get; set; }
        public DM3058ENiVisa(UseDevice device) : base("USB0::0x1AB1::0x09C4::"+device.parameter+ "::INSTR", 1500)
        {
            Name = device.name;
        }
        public Result<double> GetVoltage(int channel = 1, CeLiangYiEnum.VoltageMode mode = CeLiangYiEnum.VoltageMode.DC)
        {
            lock (this)
            {
                var result = new Result<double>();
                var write = new Result();
                var read = new Result<string>();
                if (mode == CeLiangYiEnum.VoltageMode.DC)
                {
                    AppendInfo(Name, $"打开直流电压测量");
                    write = Write(CmdSetDcVol);
                    if (!write.IsSucceed)
                    {
                        result.IsSucceed = false;
                        result.Err = "打开直流电压测量失败";
                        AppendTip(Name, result.Err);
                        return result.EndTime();
                    }
                    Thread.Sleep(100);
                    //发送指令并返回
                    read = WriteThenRead(CmdGetFunc);
                    if (!read.IsSucceed || !read.Value.Contains(CmdSetDcVolOk))
                    {
                        result.IsSucceed = false;
                        result.Err = "打开直流电压测量失败";
                        AppendTip(Name, result.Err);
                        return result.EndTime();
                    }
                    AppendInfo(Name, $"读取当前电压");
                    double vol = 0f;
                    //发送指令并返回
                    read = WriteThenRead(CmdMeasureDcVol);
                    if (!read.IsSucceed || !double.TryParse(read.Value, out vol))
                    {
                        result.IsSucceed = false;
                        result.Err = "读取当前电压失败";
                        AppendTip(Name, result.Err);
                        return result.EndTime();
                    }
                    result.Value = vol;
                    AppendResult(Name, DataConvert.ToUnit(vol, "F3") + "V");
                }
                if (mode == CeLiangYiEnum.VoltageMode.AC)
                {
                    AppendInfo(Name, $"打开交流电压测量");
                    write = Write(CmdSetAcVol);
                    if (!write.IsSucceed)
                    {
                        result.IsSucceed = false;
                        result.Err = "打开交流电压测量失败";
                        AppendTip(Name, result.Err);
                        return result.EndTime();
                    }
                    Thread.Sleep(100);
                    //发送指令并返回
                    read = WriteThenRead(CmdGetFunc);
                    if (!read.IsSucceed || !read.Value.Contains(CmdSetAcVolOk))
                    {
                        result.IsSucceed = false;
                        result.Err = "打开交流电压测量失败";
                        AppendTip(Name, result.Err);
                        return result.EndTime();
                    }
                    AppendInfo(Name, $"读取当前电压");
                    double vol = 0f;
                    //发送指令并返回
                    read = WriteThenRead(CmdMeasureAcVol);
                    if (!read.IsSucceed || !double.TryParse(read.Value, out vol))
                    {
                        result.IsSucceed = false;
                        result.Err = "读取当前电压失败";
                        AppendTip(Name, result.Err);
                        return result.EndTime();
                    }
                    result.Value = vol;
                    AppendResult(Name, DataConvert.ToUnit(vol, "F3") + "V");
                }
                return result.EndTime();
            }
        }
        public Result<double> GetCurrent(int channel = 1, CeLiangYiEnum.CurrentMode mode=CeLiangYiEnum.CurrentMode.DC)
        {
            lock (this)
            {
                var result = new Result<double>();
                var write = new Result();
                var read = new Result<string>();
                if (mode == CeLiangYiEnum.CurrentMode.DC)
                {
                    AppendInfo(Name, $"打开直流电流测量");
                    write = Write(CmdSetDcCur);
                    if (!write.IsSucceed)
                    {
                        result.IsSucceed = false;
                        result.Err = "打开直流电流测量失败";
                        AppendTip(Name, result.Err);
                        return result.EndTime();
                    }
                    Thread.Sleep(100);
                    //发送指令并返回
                    read = WriteThenRead(CmdGetFunc);
                    if (!read.IsSucceed || !read.Value.Contains(CmdSetDcCurOk))
                    {
                        result.IsSucceed = false;
                        result.Err = "打开直流电流测量失败";
                        AppendTip(Name, result.Err);
                        return result.EndTime();
                    }
                    AppendInfo(Name, $"读取当前电流");
                    double cur = 0f;
                    //发送指令并返回
                    read = WriteThenRead(CmdMeasureDcCur);
                    if (!read.IsSucceed || !double.TryParse(read.Value, out cur))
                    {
                        result.IsSucceed = false;
                        result.Err = "读取当前电流失败";
                        AppendTip(Name, result.Err);
                        return result.EndTime();
                    }
                    result.Value = cur;
                    AppendResult(Name, DataConvert.ToUnit(cur, "F3") + "A");
                }
                if (mode == CeLiangYiEnum.CurrentMode.AC)
                {
                    AppendInfo(Name, $"打开交流电流测量");
                    write = Write(CmdSetAcCur);
                    if (!write.IsSucceed)
                    {
                        result.IsSucceed = false;
                        result.Err = "打开交流电流测量失败";
                        AppendTip(Name, result.Err);
                        return result.EndTime();
                    }
                    Thread.Sleep(100);
                    //发送指令并返回
                    read = WriteThenRead(CmdGetFunc);
                    if (!read.IsSucceed || !read.Value.Contains(CmdSetAcCurOk))
                    {
                        result.IsSucceed = false;
                        result.Err = "打开交流电流测量失败";
                        AppendTip(Name, result.Err);
                        return result.EndTime();
                    }
                    AppendInfo(Name, $"读取当前电流");
                    double cur = 0f;
                    //发送指令并返回
                    read = WriteThenRead(CmdMeasureAcCur);
                    if (!read.IsSucceed || !double.TryParse(read.Value, out cur))
                    {
                        result.IsSucceed = false;
                        result.Err = "读取当前电流失败";
                        AppendTip(Name, result.Err);
                        return result.EndTime();
                    }
                    result.Value = cur;
                    AppendResult(Name, DataConvert.ToUnit(cur, "F3") + "A");
                }
                return result.EndTime();
            }
        }

        public Result<double> GetResistance(int channel = 1, CeLiangYiEnum.ResistanceMode mode = CeLiangYiEnum.ResistanceMode._2WR)
        {
            lock (this)
            {
                var result = new Result<double>();
                var write = new Result();
                var read = new Result<string>();
                if (mode == CeLiangYiEnum.ResistanceMode._2WR)
                {
                    AppendInfo(Name, $"打开二线电阻测量");
                    write = Write(CmdSet2wr);
                    if (!write.IsSucceed)
                    {
                        result.IsSucceed = false;
                        result.Err = "打开二线电阻测量失败";
                        AppendTip(Name, result.Err);
                        return result.EndTime();
                    }
                    Thread.Sleep(100);
                    //发送指令并返回
                    read = WriteThenRead(CmdGetFunc);
                    if (!read.IsSucceed || !read.Value.Contains(CmdSet2wrOk))
                    {
                        result.IsSucceed = false;
                        result.Err = "打开二线电阻测量失败";
                        AppendTip(Name, result.Err);
                        return result.EndTime();
                    }
                    AppendInfo(Name, $"读取当前电阻");
                    double r = 0f;
                    //发送指令并返回
                    read = WriteThenRead(CmdMeasure2wr);
                    if (!read.IsSucceed || !double.TryParse(read.Value, out r))
                    {
                        result.IsSucceed = false;
                        result.Err = "读取当前电阻失败";
                        AppendTip(Name, result.Err);
                        return result.EndTime();
                    }
                    result.Value = r;
                    AppendResult(Name, DataConvert.ToUnit(r, "F3") + "Ω");
                }
                if (mode == CeLiangYiEnum.ResistanceMode._4WR)
                {
                    AppendInfo(Name, $"打开四线电阻测量");
                    write = Write(CmdSet4wr);
                    if (!write.IsSucceed)
                    {
                        result.IsSucceed = false;
                        result.Err = "打开四线电阻测量失败";
                        AppendTip(Name, result.Err);
                        return result.EndTime();
                    }
                    Thread.Sleep(100);
                    //发送指令并返回
                    read = WriteThenRead(CmdGetFunc);
                    if (!read.IsSucceed || !read.Value.Contains(CmdSet4wrOk))
                    {
                        result.IsSucceed = false;
                        result.Err = "打开四线电阻测量失败";
                        AppendTip(Name, result.Err);
                        return result.EndTime();
                    }
                    AppendInfo(Name, $"读取当前电阻");
                    double r = 0f;
                    //发送指令并返回
                    read = WriteThenRead(CmdMeasure2wr);
                    if (!read.IsSucceed || !double.TryParse(read.Value, out r))
                    {
                        result.IsSucceed = false;
                        result.Err = "读取当前电阻失败";
                        AppendTip(Name, result.Err);
                        return result.EndTime();
                    }
                    result.Value = r;
                    AppendResult(Name, DataConvert.ToUnit(r, "F3") + "Ω");
                }
                return result.EndTime();
            }
        }
        public Result<double> GetContinuity(int channel = 1)
        {
            lock (this)
            {
                Result<double> result = new Result<double>();
                var write = new Result();
                var read = new Result<string>();
                AppendInfo(Name, $"打开通断测量");
                write = Write(CmdSetCont);
                if (!write.IsSucceed)
                {
                    result.IsSucceed = false;
                    result.Err = "打开通断测量失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                Thread.Sleep(100);
                //发送指令并返回
                read = WriteThenRead(CmdGetFunc);
                if (!read.IsSucceed || !read.Value.Contains(CmdSetContOk))
                {
                    result.IsSucceed = false;
                    result.Err = "打开通断测量失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                AppendInfo(Name, $"读取当前电阻");
                double r = 0f;
                //发送指令并返回
                read = WriteThenRead(CmdMeasureCont);
                if (!read.IsSucceed || !double.TryParse(read.Value, out r))
                {
                    result.IsSucceed = false;
                    result.Err = "读取当前电阻失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                result.Value = r;
                AppendResult(Name, DataConvert.ToUnit(r, "F3") + "Ω");
                return result.EndTime();
            }
        }
        public Result<double> GetDiode(int channel = 1)
        {
            lock (this)
            {
                Result<double> result = new Result<double>();
                var write = new Result();
                var read = new Result<string>();
                AppendInfo(Name, $"打开二极管测量");
                write = Write(CmdSetDiode);
                if (!write.IsSucceed)
                {
                    result.IsSucceed = false;
                    result.Err = "打开二极管测量失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                Thread.Sleep(100);
                //发送指令并返回
                read = WriteThenRead(CmdGetFunc);
                if (!read.IsSucceed || !read.Value.Contains(CmdSetDiodeOk))
                {
                    result.IsSucceed = false;
                    result.Err = "打开二极管测量失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                AppendInfo(Name, $"读取当前电压");
                double r = 0f;
                //发送指令并返回
                read = WriteThenRead(CmdMeasureDiode);
                if (!read.IsSucceed || !double.TryParse(read.Value, out r))
                {
                    result.IsSucceed = false;
                    result.Err = "读取当前电压失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                result.Value = r;
                AppendResult(Name, DataConvert.ToUnit(r, "F3") + "V");
                return result.EndTime();
            }
        }
        float RMin = 0;
        float RMax = 10000000;

        float VolMin = 0;
        float VolMax = 1000f;
        float CurMin = 0;
        float CurMax = 10f;
        string CmdSetDcVol => ":FUNCtion:VOLTage:DC\r\n";
        string CmdSetDcVolOk => "DCV";
        string CmdMeasureDcVol => ":MEASure:VOLTage:DC?\r\n";
        string CmdSetDcCur => ":FUNCtion:CURRent:DC\r\n";
        string CmdSetDcCurOk => "DCI";
        string CmdMeasureDcCur => ":MEASure:CURRent:DC?\r\n";
        string CmdSetAcVol => ":FUNCtion:VOLTage:AC\r\n";
        string CmdSetAcVolOk => "ACV";
        string CmdMeasureAcVol => ":MEASure:VOLTage:AC?\r\n";
        string CmdSetAcCur => ":FUNCtion:CURRent:AC\r\n";
        string CmdSetAcCurOk => "ACI";
        string CmdMeasureAcCur => ":MEASure:CURRent:AC?\r\n";
        string CmdSet4wr => ":FUNCtion:FRESistance\r\n";
        string CmdSet4wrOk => "4WR";
        string CmdMeasure4wr => ":MEASure:FRESistance?\r\n";
        string CmdSet2wr => ":FUNCtion:RESistance\r\n";
        string CmdSet2wrOk => "2WR";
        string CmdMeasure2wr => ":MEASure:RESistance?\r\n";

        string CmdSetCont => ":FUNCtion:CONTinuity\r\n";
        string CmdSetContOk => "CONT";
        string CmdMeasureCont => ":MEASure:CONTinuity?\r\n";

        string CmdSetDiode => ":FUNCtion:DIODe\r\n";
        string CmdSetDiodeOk => "DIOD";
        string CmdMeasureDiode => ":MEASure:DIODe?\r\n";
        string CmdGetFunc => ":FUNCtion?\r\n";
    }
}
